﻿function CalcWord(obj){
	remain = 500 - obj.value.length;
	document.getElementById("RemainingWord").innerHTML = remain;
}

function SendNotify(src){
	src.document.getElementById("CommentBox").value = '';
	src.document.getElementById("StatusText").innerHTML = '发送完毕';
	src.document.getElementById("RemainingWord").innerHTML = "500";
}

function HexieNotify(src){
	src.document.getElementById("StatusText").innerHTML = '请注意和谐。';
}

function SendRefresh(){
	setTimeout("FetchNew()", 500);
	return true;
}

function getxmlhttp(){
	var xmlhttp=false;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch(e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch(e) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

function FetchNew(){
	xmlhttp = getxmlhttp();
	xmlhttp.open("GET","wall.php?ajax=1&key="+key+"&last_id="+last_id+"&reqtime="+reqtime);
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			if (xmlhttp.responseText.substr(0,5) == '<?xml') {
				SetAndDelete(xmlhttp.responseXML);
				PraseData(xmlhttp.responseXML);
			}
		}
	}
	xmlhttp.send(null);
}

function SetAndDelete(xml){
	reqtime = xml.getElementsByTagName("namiko").item(0).getAttribute("timestamp");
	del = xml.getElementsByTagName("namiko")[0].getElementsByTagName("deletion");
	for (var i=0; i<del.length; i++){
		RemoveDeleted(del.item(i).getAttribute("id"));
	}
}

function PraseData(xml){
	comments = xml.getElementsByTagName("namiko")[0].getElementsByTagName("comment");
	for (var i=comments.length-1; i>=0; i--) {
		comment = comments.item(i);
		id = comment.attributes[0].value;
		if(i==0) last_id = id;
		if(first_id == 0 && i==comments.length-1) first_id = id;
		time = comment.attributes[1].value;
		comment = comment.text;
		FadeInComment(id,time,comment,"afterBegin");
	}
	if(admin){
		AutoRefresh = setTimeout("FetchNew()", 1000*adminrefresh);
	}else{
		AutoRefresh = setTimeout("FetchNew()", 1000*refresh);
	}
	FadeOutComment();
}

function PraseExtraData(xml){
	box = document.getElementById("WallContainer");
	box.removeChild(box.lastChild); // Remove AddMoreLink
	comments = xml.getElementsByTagName("namiko")[0].getElementsByTagName("comment");
	for (var i=0; i<comments.length; i++) {
		comment = comments.item(i);
		id = comment.attributes[0].value;
		if(i==comments.length-1) first_id = id;
		time = comment.attributes[1].value;
		comment = comment.text;
		FadeInComment(id,time,comment,"beforeEnd");
	}
	displayed = displayed + comments.length;
	FadeOutComment();
}

function FadeInComment(id,time,comment,type){
	box = document.getElementById("WallContainer");
	text = document.createElement("DIV");
	text.className = "CommentBody";
	text.id = "Msg"+id;
	if(admin){
		text.innerHTML = '<a href="javascript:void('+id+');" class="DelLink" onclick="DoDelete('+id+')">[删]</a><span class="CommentTime">'+ time + '</span><span class="CommentText">' + comment +'</span>';
	}else{
		text.innerHTML = '<span class="CommentTime">'+ time + '</span><span class="CommentText">' + comment +'</span>';
	}
	box.insertAdjacentElement(type,text);
}

function FadeOutComment(){
	box = document.getElementById("WallContainer");
	while(box.children.length > limit + displayed){
		box.removeChild(box.lastChild);
	}
	if(box.children.length == limit + displayed){
		AddMoreLink();
	}
}

function AddMoreLink(){
	box = document.getElementById("WallContainer");
	more = document.createElement("DIV");
	more.id = "MoreComment";
	more.innerHTML = '<a href="javascript:void(0)" onclick="GetMoreComment()">更多...</a>';
	box.appendChild(more);
}

function GetMoreComment(){
	xmlhttp = getxmlhttp();
	xmlhttp.open("GET","wall.php?ajax=1&key="+key+"&first_id="+first_id+"&reqtime="+reqtime);
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			if (xmlhttp.responseText.substr(0,5) == '<?xml') {
				SetAndDelete(xmlhttp.responseXML);
				PraseExtraData(xmlhttp.responseXML);
			}
		}
	}
	xmlhttp.send(null);	
}

function DoDelete(id){
	xmlhttp = getxmlhttp();
	xmlhttp.open("POST","wall.php?admin="+adminkey);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			if (xmlhttp.responseText == '1') {
				RemoveDeleted(id);
			}
		}
	}
	xmlhttp.send("cid="+id);	
}

function RemoveDeleted(id){
	try{
		document.getElementById("WallContainer").removeChild(document.getElementById("Msg"+id));
		displayed = displayed - 1;
	}catch(e){
	
	}
}